<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title>EW</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>	
        <style type="text/css">
            * {margin:0;padding:0}
            body {font:12px/16px Arial;padding:10px}
            hr {background: none;border:0;border-bottom: 1px solid #ccc; margin:5px 0}
            th {text-align: left}
            input {padding:3px 5px}
            
            .datagrid tr:hover {background: #eee}
            .datagrid .action .button {display:none}
            .datagrid tr:hover .action .button{display:inline}
        </style>        
    </head>
    <body>
        <div id="wrapper">
            <div id="content">
                <a href="<?php echo $this->config->item('base_url');?>">Home</a>
                <hr />
                <div>
                    <form>
                        sample data: <input name="sampleData" style="width:600px" value="<?php if (isset($user['sampleUrl']))
    echo $user['sampleUrl'] ?>" />
                        <input type="submit" value="Submit"/>

                    </form>
                </div>
                <div id="buttonrow">
                    <input type="button" value="Load world players" onclick="loadWorldPlayers(this)"/>
                    <input type="button" value="Load opponents" onclick="loadOpponentPlayers(this)"/>
                    <input type="button" value="Load alliances" onclick="loadAttackerAlliance(this)"/>
                    <input type="button" value="Stop" onclick="stop(this)" />
                </div>
                <hr />
                <div>
                    <?php if (isset($user['id'])):?>
                    User: <?php echo $user['name'];?>
                    <?php endif?>
                    <form id="viewdata" action="index/viewData">
                        Minimum resource:
                        <select name="min_res">
                            <option value="-1">any</option>
                            <option value="1000000">1m</option>
                            <option value="6000000">6m</option>
                            <option value="8000000">8m</option>
                            <option value="10000000" selected>10m</option>
                            <option value="12000000">12m</option>
                        </select>
                        Last updated:
                        <select name="lastupdate">
                            <option value="30">30 min</option>
                            <option value="60" selected>60 min</option>
                            <option value="any">anytime</option>
                        </select>
                        
                        <label><input type="checkbox" name="not_damage_protection" checked="checked"/> Not protection</label>
                        <label><input type="checkbox" name="offline" checked="checked"/> Offline</label>
                        <label><input type="checkbox" name="not_friend" checked="checked"/> Exclude friends</label>
                        <input type="button" value="View data" onclick="viewData(this)"/>
                    </form>
                </div>
                <hr />
                <div id="console">
                    
                </div>
            </div>
        </div>	
    </body>
</html>

<script type="text/javascript">
stopFlag = false;
threadNum = 4;
var xxx = null;

function stop(sender) {
    $(sender).attr('disabled', true);
    $('#buttonrow input[type=button]').attr('disabled', false);
    stopFlag = true;
}

function loadWorldPlayers(sender) {
    $(sender).attr('disabled', true);
    stopFlag = false;
    $('#console').html('Loading world players... <br/>');
    $.ajax({
        url:'index/getWorldPlayers',
        success:function(data){
            idList = data.split(',');
            var arr = new Array();
            for (i in idList) {
                arr[arr.length] = {id:idList[i], extraData:'src=world'};
            }
            
            updatePlayerList(arr);
            updatePlayerList(arr);
            updatePlayerList(arr);
        }
    });
    /**/
}

function loadOpponentPlayers(sender) {
    $(sender).attr('disabled', true);
    stopFlag = false;
    $('#console').html('Loading opponent players... <br/>');
    $.ajax({
        url:'index/getOpponentPlayers',
        success:function(data){
            idList = data.split(',');
            var arr = new Array();
            for (i in idList) {
                arr[arr.length] = {id:idList[i], extraData:'src=opponent'};
            }
            updatePlayerList(arr);
            updatePlayerList(arr);
            updatePlayerList(arr);
            
        }
    });
    /**/
}

var globalPlayer = null;

function loadAttackerAlliance(sender) {
    $(sender).attr('disabled', true);
    stopFlag = false;
    $('#console').html('Loading possible alliances... <br/>');
    $.ajax({
        url:'index/getAttackerAlliance',
        dataType:'json',
        success:function(data){
            //convert to array
            var arr = new Array();
            for (i in data) {
                arr[arr.length++] = data[i];
                arr[arr.length-1]['id'] = i;
            }
            
            $('#console').html('Loading players from alliances... <br/>');
            globalPlayer = new Array();
            //console.info(arr[0]);
            loadPlayersFromAlliance(arr, 0);    
        }
    });
}

function loadPlayersFromAlliance(arr, current) {
    var item=arr[current];
    
    $('#console').append('Updating alliance #'+(current+1)+'/'+arr.length+' alliance '+item['name'] + '...');
    
    $.ajax({
        url:'index/getAllianceData',
        data:'id='+item.id,
        dataType:'json',
        success:function(data){
            $('#console').append(data.members.length + ' members <br />');
            for (i in data.members) {
                globalPlayer[globalPlayer.length] = {id:data.members[i].id, extraData:'src='+item.source};
            }
            
            //item.memberIdList = idArray;
            //console.info(idArray);
            //idArray = data.split(',');
            //updatePlayerList(idArray, 0, 'src=opponent');
        },
        complete:function() {
            if (current<arr.length-1) {
                if (!stopFlag)
                    loadPlayersFromAlliance(arr, current+1);
                else
                    stopFlag = false;
            }
            else {
                $('#console').append('Done <br />');
                //console.info(globalPlayer.length);
                //console.info(globalPlayer[0]);
                updatePlayerList(globalPlayer);
                updatePlayerList(globalPlayer);
                updatePlayerList(globalPlayer);
            }
        }
    });    
    /**/
}

function updatePlayerList(arr) {
    //find 1st available item
    item = null;
    found = 0;
    for (i in arr) {
        if (arr[i].dirty) 
            found++;
        else if (item==null) {
            item = arr[i];arr[i].dirty = true;
        }
    }
    
    if (item == null) {
        $('#console').append('Done <br />');
        $('#buttonrow input[type=button]').attr('disabled', false);
        return;
    }
    else if (stopFlag) {
        $('#buttonrow input[type=button]').attr('disabled', false);
        return ;
    }
    
    id = item.id;
    extraData = item.extraData;
    //$('#console').append('Updating no#'+(found+1)+'/'+arr.length+' player id#'+id + '...');
    $.ajax({
        url:'index/updatePlayer',
        data:'id='+id+'&'+extraData,
        dataType:'json',
        success:function(data){
            var html = data.name+', '+data.level;
            $('#console').append('Updated #'+(found+1)+'/'+arr.length+' player id#'+id + ' '+html+' <br />');
            //$('#console').append('<br />');
            //$('#console').append(html +'<br />');
            
        },
        complete:function() {
            updatePlayerList(arr);
        }
    });
}


function viewData(sender, sortBy) {
    formData = $('#viewdata').serialize();
    formData += '&sort=' + sortBy;
    $(sender).attr('disabled', true);
    $.ajax({
        url:'index/viewData',
        data:formData,
        success:function(html){
            $('#console').html(html);
        },
        complete:function() {
            $(sender).attr('disabled', false);
        }
    });
}

</script>